<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ShoppingCartController extends Controller
{
   public function shopping_cart(Request $request)
   {
       //分配session信息到主页模板
       $user_info= session('sign_user_info');

       //当用户没有登录时 添加商品到购物车时，将session中的数据分配到购物车列表
       $add_shopping_cart = session('shopping_cart');


       $res = $request->method();

       if($res == 'GET')
       {
           if(empty($user_info))
           {
               return view('Home/ShoppingCart/shopping_cart')->with('add_shopping_cart',$add_shopping_cart);

           }else{

               //当用户登录时 数据库中暂存的购物车中的信息 添加到购物车列表
               $goods_list = DB::table('shopping_cart')
                   ->where('shopping_cart.user_id','=',$user_info['id'])
                   ->join('goods','shopping_cart.goods_id','=','goods.goods_id')
                   ->join('img','shopping_cart.goods_id','=','img.goods_id')
                   ->orderBy('shopping_cart.add_time','desc')
                   ->get();
               print_r($goods_list);
               return view('Home/ShoppingCart/shopping_cart')->with('goods_list',$goods_list)->with('user_info',$user_info);
           }
       }

       if($res == 'POST')
       {
           $data = $request->input();
           $goods_id = $data['goods_id'];
           $goods_count = $data['goods_count'];
           $property = $data['property'];
           $property = implode(':',$property);

           //对ajax发过来的数据判断 将数据添加到购物车列表
           if(empty($goods_id))
           {
               return ['status'=>0,'info'=>'非法操作'];
           }
           if(empty($goods_count) || (int)$goods_count < 1)
           {
               return ['status'=>0,'info'=>'商品数量信息有误'];
           }
           //去重验证
           $goods = DB::table('shopping_cart')->select('goods_id')->where(['goods_id'=>$goods_id,'user_id'=>$user_info['id']])->first();
           if($goods)
           {
               return ['status'=>0,'info'=>'该商品已存在购物车'];
           }
           //根据用户是否session信息来判断用户是否登录,如果用户没有登录，则将添加到购物车的商品信息写入session中
           //如果用户是已登录状态则把添加到购物车的商品信息写入数据库
           if(empty($user_info))
           {
              //当用户还没有登录 选择商品到购物车时

               if(empty(session('shopping_cart')))
               {
                   session(['shopping_cart'=>[$data]]);
               }else{
                   $data1 = session('shopping_cart');
                   array_push($data1,$data);
                   session(['shopping_cart'=>$data1]);
               }

           }else{
               $user_info = session('sign_user_info');
               //获取购物车的session信息
               $add_shopping_cart = session('shopping_cart');
               //如果session中有购物车的数据，把该数据添加到用户的购物车数据库表中
               if(isset($add_shopping_cart))
               {
                   $aaa = [];
                   foreach ($add_shopping_cart as $v)
                   {
                       $tmp = [];
                       $tmp['goods_id'] = $v['goods_id'];
                       $tmp['goods_count'] = $v['goods_count'];
                       $tmp['user_id'] = $user_info['id'];
                       $tmp['property'] = implode(':',$v['property']);
                       $aaa[] = $tmp;
                   }
                   $goods = DB::table('shopping_cart')
                       ->insert($aaa);
                   if($goods)
                   {
                       $request->session()->forget('shopping_cart');
                   }
               }

               $shopping_cart = DB::table('shopping_cart')
                   ->insert([
                       'user_id'=>$user_info['id'],
                       'goods_id'=>$goods_id,
                       'goods_count'=>$goods_count,
                       'property'=>$property,
                   ]);

               if(!$shopping_cart)
               {
                   return ['status'=>0,'info'=>'添加失败'];
               }

           }

       }

   }

   //从购物车删除商品
   public function goods_delete(Request $request,$goods_id)
   {
       if(empty($goods_id))
       {
           return ['status'=>0,'info'=>'非法操作'];
       }
       $res = DB::table('shopping_cart')->where('goods_id','=',$goods_id)->delete();
       if($res)
       {
           return ['status'=>1,'info'=>'删除成功'];
       }else{
           return ['status'=>0,'info'=>'删除失败'];
       }

   }
}
